#pragma GCC optimize(2, "Ofast", "inline")
#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#include <set>
#include <queue>
#include <stack>
#include <list>
#include <unordered_map>
#include <algorithm>
#define endl '\n'
#define int long long
#define pii pair<int, int>
#define fi first
#define se second
#define int128 __int128_t
using namespace std;

struct  node
{
    int p = -1;
    node* next = nullptr;
};

struct mylist
{
    node* head = nullptr;
    node* tail = nullptr;
};

// 已知
int n, p, q, t;
string s;
mylist pcs[200005];
mylist ser;
vector<string> ans;

// 未知


signed main()
{
    cin.tie(0)->ios::sync_with_stdio(false);
    cin >> n >> q;
    for (int i = 0;i < q;++i)
    {
        cin >> t >> p;
        if (t == 1)
        {
            pcs[p].head = ser.head;
            pcs[p].tail = ser.tail;
        }
        else if (t == 2)
        {
            cin >> s;
            ans.push_back(s);
            node* tmp = new node;
            tmp->p = ans.size() - 1;
            tmp->next = nullptr;
            if (pcs[p].head == nullptr) pcs[p].head = tmp;
            else pcs[p].tail->next = tmp;
            pcs[p].tail = tmp;
        }
        else
        {
            ser.head = pcs[p].head;
            ser.tail = pcs[p].tail;
        }
    }
    auto it = ser.head;
    while (it != ser.tail and it)
    {
        cout << ans[it->p];
        it = it->next;
    }
    if (it != nullptr) cout << ans[it->p] << endl;
    return 0;
}